package com.sky.service.impl;

import com.fasterxml.jackson.databind.ser.Serializers;
import com.google.j2objc.annotations.AutoreleasePool;
import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private DishMapper dishMapper;

    /**
     * 添加购物车
     * @param shoppingCartDTO
     */
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        //首先判断添加到购物车的商品是否已经存在，如果存在，则将对一个的数量加一，否则插入
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);

        //设置用户id  //用户id区别购物车的归属
        shoppingCart.setUserId(BaseContext.getCurrentId());

        List<ShoppingCart> shoppingCartList = shoppingCartMapper.list(shoppingCart);

        //存在,更新number+1
        if(shoppingCartList != null && shoppingCartList.size() > 0){
            //该查询只可能查到一条数据或者查不到数据   因为各个id都已经确定了，最后只会查到一条结果
            ShoppingCart cart = shoppingCartList.get(0);
            cart.setNumber(cart.getNumber()+1);

            shoppingCartMapper.updateNumById(cart);
        }else {
            //不存在，则插入数据
            //判断是传过来的是套餐id还是菜品id,然后查询对应其他属性（如name,image等）
            Long setmealId = shoppingCartDTO.getSetmealId();
            Long dish_id = shoppingCartDTO.getDishId();

            if (dish_id != null){
                //添加的菜品
                Dish dish =dishMapper.getById(shoppingCartDTO.getDishId());
                shoppingCart.setAmount(dish.getPrice());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setName(dish.getName());

//                shoppingCartMapper.insert();
            }else {
                //添加的套餐
                Setmeal setmeal = setmealMapper.getById(shoppingCartDTO.getSetmealId());

                shoppingCart.setAmount(setmeal.getPrice());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setName(setmeal.getName());

            }
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCart.setNumber(1);

            //最后插入购物车数据即可
            shoppingCartMapper.insert(shoppingCart);

        }

    }

    /**
     * 清空购物车
     */
    public void clean() {
        //根据userid把该用户对应的购物车信息都删掉即可
        Long id = BaseContext.getCurrentId();
        shoppingCartMapper.cleanByUserId(id);
    }

    /**
     * 删除购物车中的一个商品
     */
    public void cleanById(ShoppingCartDTO shoppingCartDTO) {
        //直接封装成一个shoppingcart进行删除，动态sql，套餐和菜品哪个id存在删哪条，记得要传入该用户的user_id

        //后来发现，如果商品数量大于1的话，不能直接删除，所以要多一个判断

        ShoppingCart shoppingCart = ShoppingCart.builder()
                .dishId(shoppingCartDTO.getDishId())
                .setmealId(shoppingCartDTO.getSetmealId())
                .dishFlavor(shoppingCartDTO.getDishFlavor())
                .userId(BaseContext.getCurrentId())
                .build();

        //查询一下，判断数量是否为1
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);

        //由于各个id都确定了，所以实际上只有一个结果，所以取get(0)即可
        if(list != null && list.size() > 0){
            ShoppingCart cart = list.get(0);
            if(cart.getNumber() == 1){
                shoppingCartMapper.DeleteById(cart);  //数量为1直接删除
            }else {
                //否则更新数量为number-1
                cart.setNumber(cart.getNumber()-1);
                shoppingCartMapper.updateNumById(cart);
            }
        }

    }
}
